#include "CMainMenuState.h"
#include "CSpaceStationState.h"
#include "CCyberSpaceState.h"
#include "COptionsState.h"
#include "CHowToPlayState.h"
#include "CCreditsState.h"
#include "CHighScoreState.h"
#include "CSaveLoadState.h"
#include "CGame.h"
#include "Wrappers/CSGD_DirectInput.h"
#include "CPrintBitmapToScreen.h"

CMainMenuState::CMainMenuState(void)
{
	m_nCurrentSelection = 0;
	m_nCursorID = -1;
	m_nBackgroundImageID = -1;
	m_nBackgroundMusicID = -1;
	m_nMovementSFXID = -1;
}

CMainMenuState::CMainMenuState(const CMainMenuState&)
{

}

CMainMenuState& CMainMenuState::operator=(const CMainMenuState&)
{
	return *this;
}

CMainMenuState::~CMainMenuState(void)
{

}

CMainMenuState* CMainMenuState::GetInstance(void)
{
	static CMainMenuState instance;
	return &instance;
}

void CMainMenuState::Enter(void)
{
	// Load resources
	m_nCursorID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/SGP_Cursor.png");
	m_nMovementSFXID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_MenuSelection.wav");
	m_nBackgroundMusicID = CSGD_FModManager::GetInstance()->LoadSound("Resource/Sound/SGP_MenuMusic.mp3");
	m_nBackgroundImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/Art Assets/MenuArm.png");
	
	// Load the options
	COptionsState::GetInstance()->Load("Options.xml");

	// Play intro music
	CSGD_FModManager::GetInstance()->SetVolume(m_nBackgroundMusicID, COptionsState::GetInstance()->GetVolume());
	CSGD_FModManager::GetInstance()->SetVolume(m_nMovementSFXID,COptionsState::GetInstance()->GetSFXVolume());
	CSGD_FModManager::GetInstance()->PlaySoundA(m_nBackgroundMusicID);
	
}

bool CMainMenuState::Input(void)
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP))
	{
		if(m_nCurrentSelection > 0)
		{
			--m_nCurrentSelection;
		}
		else
			m_nCurrentSelection = 4;

		CSGD_FModManager::GetInstance()->SetVolume(m_nMovementSFXID,COptionsState::GetInstance()->GetSFXVolume());
		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);

	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN))
	{
		if(m_nCurrentSelection < 4)
		{
			++m_nCurrentSelection;
		}
		else
			m_nCurrentSelection = 0;

		CSGD_FModManager::GetInstance()->SetVolume(m_nMovementSFXID,COptionsState::GetInstance()->GetSFXVolume());
		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	/*if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_LEFT))
	{
		m_nCurrentSelection = 6;
		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_RIGHT))
	{
		m_nCurrentSelection = 0;
		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}*/

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
	{
		if(m_nCurrentSelection == 0)
		{
			// Play the game
			CGame::GetInstance()->ChangeState(CSpaceStationState::GetInstance());
			//CGame::GetInstance()->ChangeState(CCyberSpaceState::GetInstance());
		}
		else if(m_nCurrentSelection == 1)
		{
			// Open options menu
			CGame::GetInstance()->PushState(COptionsState::GetInstance());
		}
		else if(m_nCurrentSelection == 2)
		{
			// Show game instructions
			CGame::GetInstance()->PushState(CHowToPlayState::GetInstance());
		}
		else if(m_nCurrentSelection == 3)
		{
			// Show credits
			CGame::GetInstance()->PushState(CCreditsState::GetInstance());
		}
		else if(m_nCurrentSelection == 4)
		{
			// Exit
			return false;
		}
		/*else if(m_nCurrentSelection == 6)
		{
			CSaveLoadState::GetInstance()->SetCurrState(CSaveLoadState::LOAD);
			CGame::GetInstance()->PushState(CSaveLoadState::GetInstance());
		}*/
		if(CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBackgroundMusicID))
			CSGD_FModManager::GetInstance()->StopSound(m_nBackgroundMusicID);
	}
	return true;
}

void CMainMenuState::Update(float fElapsedTime)
{
	// If the user loaded a game, go into the cyber space state
	
	CSGD_FModManager::GetInstance()->SetVolume(m_nBackgroundMusicID, COptionsState::GetInstance()->GetVolume());
	CSGD_FModManager::GetInstance()->SetVolume(m_nMovementSFXID,COptionsState::GetInstance()->GetSFXVolume());

	if(CSaveLoadState::GetInstance()->GetLoaded())
	{

		CSaveLoadState::GetInstance()->SetLoaded(false);
		CGame::GetInstance()->ChangeState(CSpaceStationState::GetInstance());
	}
}

void CMainMenuState::Render(void)
{
	RECT rBackground;
	rBackground.left = 0;
	rBackground.top = 0;
	rBackground.right = rBackground.left + CGame::GetInstance()->GetScreenWidth();
	rBackground.bottom = rBackground.top + CGame::GetInstance()->GetScreenHeight();

	// Draw the background image
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundImageID, 0, 0, 0.65f, 0.47f);

	// Display menu options
	if(m_nCurrentSelection == 0)
		CPrintBitmapFontToScreen::GetInstance()->Print("PLAY", 320, 140, 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("PLAY", 320, 140, 1.0f, D3DCOLOR_XRGB(0,255,0));

	if(m_nCurrentSelection == 1)
		CPrintBitmapFontToScreen::GetInstance()->Print("OPTIONS", 320, 170, 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("OPTIONS", 320, 170, 1.0f, D3DCOLOR_XRGB(0,255,0));

	if(m_nCurrentSelection == 2)
		CPrintBitmapFontToScreen::GetInstance()->Print("GUIDE", 320, 200, 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("GUIDE", 320, 200, 1.0f, D3DCOLOR_XRGB(0,255,0));

	/*if(m_nCurrentSelection == 3)
		CPrintBitmapFontToScreen::GetInstance()->Print("SCORES", 320, 230, 1.0f, D3DCOLOR_XRGB(255, 0, 0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("SCORES", 320, 230, 1.0f, D3DCOLOR_XRGB(0, 255, 0));*/

	if(m_nCurrentSelection == 3)
		CPrintBitmapFontToScreen::GetInstance()->Print("CREDITS", 320, 230, 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("CREDITS", 320, 230, 1.0f, D3DCOLOR_XRGB(0,255,0));

	if(m_nCurrentSelection == 4)
		CPrintBitmapFontToScreen::GetInstance()->Print("EXIT", 320, 260, 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("EXIT", 320, 260, 1.0f, D3DCOLOR_XRGB(0,255,0));
	
	/*if(m_nCurrentSelection == 6)
		CPrintBitmapFontToScreen::GetInstance()->Print("LOAD PROFILE", 20, 20, 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("LOAD PROFILE", 20, 20, 1.0f, D3DCOLOR_XRGB(0,255,0));*/

}

void CMainMenuState::Exit(void)
{

	// Unload assets
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nCursorID);
	CSGD_FModManager::GetInstance()->UnloadSound(m_nMovementSFXID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackgroundImageID);
}